﻿using AuthCenter.Domain;
using AuthCenter.ErorCode;
using AuthCenter.Infrastructure;
using AuthCenter_API.Read.Applications.Commands.RBAC;
using AuthCenter_API.Read.Dtos;
using AutoMapper;
using MediatR;

namespace AuthCenter_API.Read.Applications.CommandHandlers.RBAC
{
    public class ParentNameQueryCommandHandler : IRequestHandler<ParentNameQueryCommand, APIResult<List<MenuDto>>>
    {
        private readonly IBaseRepository<MenuModel> _menuRepository;
        private readonly IMapper _mapper;

        public ParentNameQueryCommandHandler(IBaseRepository<MenuModel> menuRepository, IMapper mapper)
        {
            _menuRepository = menuRepository;
            _mapper = mapper;
        }

        public async Task<APIResult<List<MenuDto>>> Handle(ParentNameQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<MenuDto>> result = new APIResult<List<MenuDto>>();
            result.Code = APIEnum.QuerySuccess;
            result.Message = APIEnum.QuerySuccess.Desc();
            var m = _menuRepository.GetAll().Where(x => x.IsDel == 0&&x.PID==0);
            var menudtos = m.Select(m => new MenuDto
            {
                MenuId = m.MenuId,
                MenuName = m.MenuName,
                Url = m.Url,
                PID = m.PID,
                CreateName = m.CreateName,
                CreateTime = m.CreateTime,
                UpdateName = m.UpdateName,
                UpdateTime = m.UpdateTime,
                OrderId = m.OrderId,
                PName = m.MenuName
            }).ToList();
            result.Data= menudtos;
            return await Task.FromResult(result);
        }
    }
}
